package chapter13_threadSafe;

import chapter12_memModel.P12_2;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * AtomicInteger
 */
public class P13_1 {
    static AtomicInteger count = new AtomicInteger(0);



    private static void increase() {
        //compareAndSwapInt CAS操作 比较设置操作，有3个操作数，变量的内存，新值，旧值，首先比较一下旧值和内存中的变量是否一致，如果一致，用新值更新内存
        count.getAndIncrement();
    }
    public static void main(String[] args) {
        Thread[] threads = new Thread[20];
        for (Thread tread : threads) {
            tread = new Thread(()->{
                for (int i = 0; i < 1000; i++) {
                    increase();
                }
            });
            tread.start();
        }
        while (Thread.activeCount() > 1){
            Thread.yield();
        }
        System.out.println(count);
    }

}
